package com.zerdoor.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

/**
 * Created by xiaobo on 2014/11/5.
 */
@Controller
@RequestMapping("/acg")
public class AcgController {
    //@Autowired CookieLocaleResolver resolver;

    @Autowired SessionLocaleResolver resolver;

    /**
     * 语言切换
     */
    @RequestMapping("language")
    public ModelAndView language(HttpServletRequest request,HttpServletResponse response,String language,String url){
        language=language.toLowerCase();
        request.getSession().setAttribute("language",language);
        if(language==null||language.equals("")){
            return new ModelAndView("redirect:"+url);
        }else{
            if(language.equals("zh_cn")){
                resolver.setLocale(request, response, Locale.CHINA );
            }else if(language.equals("en")){
                resolver.setLocale(request, response, Locale.ENGLISH );
            }else{
                resolver.setLocale(request, response, Locale.CHINA );
            }
        }

        return new ModelAndView("redirect:"+url);
    }

    /**
     * 底部静态文件
     */
    @RequestMapping("fpage")
    public String fpage(HttpServletRequest request,HttpServletResponse response,String title,String url) throws UnsupportedEncodingException {
        request.setAttribute("title",java.net.URLDecoder.decode(title,"UTF-8"));
        return url;
    }
}
